import './index.less';
$(function () {
    console.log('123');
     function captureUserMedia(mediaConstraints, successCallback, errorCallback) {
         navigator.mediaDevices.getUserMedia(mediaConstraints).then(successCallback).catch(errorCallback);
     }
     var mediaConstraints = {
         audio: true
     };
     var mediaRecorder;
     var src = '';
     var file = null;
     function onMediaSuccess(stream) {
         mediaRecorder = new MediaStreamRecorder(stream);
         mediaRecorder.mimeType = 'audio/wav';
         mediaRecorder.stream = stream;
         mediaRecorder.ondataavailable = function (blob) {
             file = blob;
             src = URL.createObjectURL(blob);
         };
         mediaRecorder.start(100 * 1000);
     }

     function onMediaError(e) {
         console.error('media error', e);
     }
     $('.start').on('click', function () {
         $('.audio-wrap').toggleClass('start-mode');
         $('.audio audio').remove();
         captureUserMedia(mediaConstraints, onMediaSuccess, onMediaError);
     });
     $('.stop').on('click', function () {
         $('.audio-wrap').toggleClass('start-mode');
         mediaRecorder.stop();
         mediaRecorder.stream.stop();
         $('.audio audio').remove();
         $('.audio').append('<audio src="' + src + '" controls></audio>');
     });

     function onMediaError(e) {
         console.error('media error', e);
     }

     $('.specified-wrap').on('click', 'input', function (e) {
         if (e.target.value == 'false') {
             $('.param-wrap').hide();
         } else {
             $('.param-wrap').show();
         }
     });
     $('#submit').on('click', function () {
         if (file == null) {
             $("#myModal").modal();
             return;
         }
         var compose = $('input[name=compose][checked]').attr('value');
         var specified = $('input[name=specified][checked]').attr('value');
         var param = $('#param').length == 0 ? null : $('#param').val();
         var formData = new FormData();
         formData.append('isclient', compose);
         formData.append('islan', specified);
         formData.append('lan', param);
         formData.append('voice', file);
         console.log(file);

         $.ajax({
             url: "/qa",
             type: "POST",
             data: formData,
             processData: false,
             contentType: false,
             success: function (data) {
                 $('.output').empty().append('<audio src="' + data + '" controls autoplay="true"></audio>');
             },
             error: function () {
                 console.log("出错");
             }
         });
     })
 });


//  /*@param isclient 是否客户端负责合成语音
//  @param islan 是否允许由用户指定语言
//  @param lan 携带的语言参数
//  @param voice 上传的语音文件*/